import React, {useEffect, useState} from 'react';
import {StyleSheet, StatusBar} from 'react-native';
import {colors} from '@/config';
import SplashScreen from 'react-native-splash-screen';
import memoizeOne from 'memoize-one';
import {createRootNavigator} from '@/router';
import {NavigationContainerComponent} from 'react-navigation';
const memoizeCreateRootNavigator = memoizeOne(createRootNavigator);
import NavigationService from '@/NavigationService';
import {Header, Colors} from 'react-native/Libraries/NewAppScreen';
import {isSignedIn} from '@/utils/auth';
import '@/api';

declare const global: {HermesInternal: null | {}};

function refNavigatorFun(navigatorRef: NavigationContainerComponent) {
  NavigationService.setTopLevelNavigator(navigatorRef);
}

export default function App() {
  const [signedIn, setSignedIn] = useState(false);
  const [checkedSignIn, setCheckedSignIn] = useState(false);

  useEffect(() => {
    SplashScreen.hide();
    const firstLoad = async () => {
      const res = await isSignedIn();
      setSignedIn(res);
      setCheckedSignIn(true);
    };
    firstLoad();
  }, []);

  if (!checkedSignIn) {
    return null;
  }

  const RootNavigator = memoizeCreateRootNavigator(signedIn);
  return (
    <>
      <StatusBar
        backgroundColor={colors.primary}
        barStyle="light-content"
        animated
      />
      <RootNavigator ref={refNavigatorFun} />
    </>
  );
}

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});
